package com.samsung.list;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.samsung.R;
import com.samsung.adapter.ItemListAdapter;
import com.samsung.db.Call;
import com.samsung.db.CallDB;
import com.samsung.db.Message;
import com.samsung.db.MessageDB;
import com.samsung.db.Picture;
import com.samsung.db.PictureDB;

public class ListItemViewActivity extends ListActivity
{
	
	private int type;
	private long date;
	private ArrayList<ListItem> list;
	private ItemListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mylist);
		
		Intent intent = getIntent();
		
		type = intent.getIntExtra("type", 0);
		date = intent.getLongExtra("date", 0);
		
		list = new ArrayList<ListItem>();
		
		adapter = new ItemListAdapter(this,list);
		
		updateList();
		
		setListAdapter(adapter);
		
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
		ListItem item = list.get(position);
		Intent intent;
		
		switch(type)
		{
		
		case 1:
			intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+item.getName()));
			intent.putExtra("message_id", 3003);
			startActivity(intent);
			break;
		case 2:
			intent = new Intent(ListItemViewActivity.this,ViewMessageActivity.class);
			intent.putExtra("number", item.getName());
			intent.putExtra("body", item.getBody());
			startActivity(intent);
			break;
		case 3:
			intent = new Intent(ListItemViewActivity.this,ViewPictureActivity.class);
			intent.putExtra("filepath", item.getBody());
			startActivity(intent);
			break;
			
		}
		
	}

	private void updateList()
	{
		
		Cursor cur;
		
		list.clear();
		
		switch(type)
		{
		
		case 1:
			CallDB callDb = new CallDB(this);
			Call c = new Call();
			
			c.setSetdate(date);
			
			cur = callDb.fetchListCall(c);
			
			cur.moveToFirst();
			
			if(!cur.isAfterLast())
			{
				do
				{
					ListItem item = new ListItem();
					item.setDate(cur.getLong(6));
					item.setName(cur.getString(5));
					item.setType(type);
					list.add(item);
				}while(cur.moveToNext());
			}
			cur.close();
			callDb.close();
			break;
			
		case 2:
			MessageDB messageDb = new MessageDB(this);
			Message m = new Message();
			
			m.setSetdate(date);
			
			cur = messageDb.fetchListMessage(m);
			
			cur.moveToFirst();
			
			if(!cur.isAfterLast())
			{
				do
				{
					ListItem item = new ListItem();
					item.setDate(cur.getLong(7));
					item.setName(cur.getString(6));
					item.setType(type);
					item.setBody(cur.getString(5));
					list.add(item);
				}while(cur.moveToNext());
			}
			
			break;
			
		case 3:
			PictureDB pictureDb = new PictureDB(this);
			Picture p = new Picture();
			
			p.setSetdate(date);
			
			cur = pictureDb.fetchListPicture(p);
			
			cur.moveToFirst();
			
			if(!cur.isAfterLast())
			{
				do
				{
					ListItem item = new ListItem();
					String body = cur.getString(5);
					int len = body.split("/").length;
					item.setDate(cur.getLong(7));
					item.setName(body.split("/")[len-1]);
					item.setBody(body);
					item.setType(type);
					list.add(item);
				}while(cur.moveToNext());
			}
			break;
			
		}
		adapter.forceReload();
	}
	
}